package com.yixun.controller;

import com.yixun.entity.YxProCultured;
import com.yixun.entity.YxUser;
import com.yixun.service.YxProCulturedService;
import com.yixun.util.ResponseModel;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 畜禽种类管理(YxProCultured)表控制层
 *
 * @author lbx
 * @since 2021-06-16 09:00:29
 */
@Controller
@RequestMapping("yxProCultured")
public class YxProCulturedController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private YxProCulturedService yxProCulturedService;
    
    @GetMapping("index")
    public String goIndex() {
        return "YxProCultured/index";
    }
    
    @GetMapping("addPage")
    public String addAndUpdatePage(@Nullable Integer culturedId, Model model) {
        if (culturedId != null) {
            System.out.println(culturedId);
            model.addAttribute("culturedId", culturedId);
        }
        return "YxProCultured/addPage";
    }
    
    /**
     * 插入单条数据
     *
     * @param yxProCultured 数据实体类封装
     * @return 单条数据
     */
    @PostMapping("insertOne")
    @ResponseBody
    public Map<String, Object> insertOne(@RequestBody YxProCultured yxProCultured) {
        YxUser yxUser = this.getSessionUser();
        yxProCultured.setAddUser(yxUser.getUser_id());
        int i = yxProCulturedService.insert(yxProCultured);
        
        return ResponseModel.getModel("success", "200", i);
    }
    
    
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne/{id}")
    @ResponseBody
    public Map<String, Object> selectOne(@PathVariable Long id) {
        return ResponseModel.getModel("success", "200", this.yxProCulturedService.queryById(id));
    }
    
    /**
     * 分页查询
     *
     * @param map start_num 当前页 page_size 每页条数
     * @return yxProCultureds 数据集合 list
     */
    @PostMapping("getList")
    @ResponseBody
    public Map<String, Object> selectListByPage(@RequestBody HashMap<String, Integer> map) {
        List<YxProCultured> yxProCultureds = yxProCulturedService.queryAllByLimit(map.get("start_num"), map.get("page_size"));
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("list", yxProCultureds);
        map1.put("count", this.getCount());
        return ResponseModel.getModel("success", "200", map1);
    }
    
    @PostMapping("/getCount")
    @ResponseBody
    public Integer getCount() {
        return yxProCulturedService.getCount();
    }
    
    /**
     * 通过id更新单条数据  cultured.id  not null
     *
     * @param cultured 封装好的实体
     * @return yxProCultured 更新后的数据
     */
    @PostMapping("updateOneById")
    @ResponseBody
    public Map<String, Object> updateOneById(@RequestBody YxProCultured cultured) {
        if (cultured.getCulturedId() == null) {
            return ResponseModel.getModel("failure，id不能为空", "200", null);
        }
        YxUser yxUser = this.getSessionUser();
        cultured.setUpdateUser(yxUser.getUser_id());
        YxProCultured yxProCultured = yxProCulturedService.update(cultured);
        return ResponseModel.getModel("success", "200", yxProCultured);
    }
    
    /**
     * 通过主键删除数据 将 is_delete 修改为 -1
     *
     * @param id 主键
     * @return int 受影响数据数
     * @apiNote 请求数据示例  {"ids":"1,2,3,4"}
     */
    @PostMapping("updateDelete/{id}")
    @ResponseBody
    public Map<String, Object> updateDelete(@PathVariable Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("updateUser", this.getSessionUser().getUser_id());
        map.put("culturedId", id);
        int i = this.yxProCulturedService.updateIsDelete(map);
        if (i > 1) {
            return ResponseModel.getModel("success", "200", i);
        }
        return ResponseModel.getModel("failure", "400", i);
    }
    
    
    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @PostMapping("/queryAll")
    @ResponseBody
    public List<YxProCultured> queryAll() {
        return yxProCulturedService.queryAll();
    }
    
    /**
     * @param map 条件 字段 封装
     * @return java.util.List<com.yixun.entity.YxSurveyXqRecord>
     * @apiNote 条件搜索
     * @author 吕兵星
     * @date 2021/6/27 20:21
     */
    @RequestMapping("getSearchList")
    @ResponseBody
    public HashMap<String, Object> getSearchList(@RequestBody HashMap<String, Object> map) {
        System.err.println(map);
        ArrayList<String> culturedCodeIds = null;
        ArrayList<String> culturedTypes = null;
        String culturedName = null;
        Integer offset = null;
        Integer limit = null;
        try {
            culturedCodeIds = (ArrayList<String>) map.get("culturedCodeId");
            culturedTypes = (ArrayList<String>) map.get("culturedType");
            culturedName = (String) map.get("culturedName");
            offset = (Integer) map.get("start_num");
            limit = (Integer) map.get("page_size");
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.clear();
        if (culturedCodeIds.size() > 0) {
            map.put("culturedCodeId", culturedCodeIds.get(culturedCodeIds.size() - 1));
        }
        if (culturedTypes.size() > 0) {
            map.put("culturedType", culturedTypes.get(culturedTypes.size() - 1));
        }
        map.put("offset", offset);
        map.put("limit", limit);
        map.put("culturedName", culturedName);
        System.err.println(map);
        List<YxProCultured> list = yxProCulturedService.searchAll(map);
        int total = yxProCulturedService.getSearchCount(map);
        map.put("items", list);
        map.put("total", total);
        return map;
    }
    
}
